﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ObjectPool : MonoSingleton<ObjectPool> {
    private Dictionary<string, Queue<GameObject>> pool = new Dictionary<string, Queue<GameObject>>();
    /// <summary>
    /// 从对象池获得物体
    /// </summary>
    /// <param name="go"></param>
    /// <returns></returns>
    public GameObject OutPool(GameObject go,bool active=true)
    {
        //为了去掉（clone）后缀
        string key= go.name.Split('(')[0];
        if (pool.ContainsKey(key))
        {
            if (pool[key].Count > 0)
            {
                GameObject g = pool[key].Dequeue();
                g.SetActive(active);
                g.transform.SetParent(go.transform.parent);
                if (!g.activeInHierarchy)
                {
                    //如果是加载出来的就需要实例化
                    return Instantiate(g);
                }
                return g;
            }
            GameObject clo = Instantiate(go);
            clo.transform.SetParent(go.transform.parent);
            return clo;
        }
        Queue<GameObject> que = new Queue<GameObject>();
        pool.Add(key, que);
        GameObject clone = Instantiate(go);
        clone.transform.SetParent(go.transform.parent);
        return clone;
    }
    /// <summary>
    /// 通过路径(Resources文件夹下)获得对象，没有就加载
    /// </summary>
    /// <param name="name">路径</param>
    /// <param name="parent"></param>
    /// <param name="active"></param>
    /// <returns></returns>
    public GameObject OutPool(string filePath,Transform parent=null,bool active = true)
    {
        string[] k = filePath.Split('/');
        string key = k[k.Length - 1];
        if (pool.ContainsKey(key))
        {
            if (pool[key].Count > 0)
            {
                GameObject g = pool[key].Dequeue();
                g.SetActive(active);
                if (parent)
                {
                    g.transform.SetParent(parent);
                }
                if (!g.activeInHierarchy)
                {
                    //如果是加载出来的就需要实例化
                    return Instantiate(g);
                }
                return g;
            }
        }
        else
        {
            Queue<GameObject> que = new Queue<GameObject>();
            pool.Add(key, que);
        }
        GameObject clone=ResourcesManager.Instance.Load<GameObject>(filePath);
        if (clone)
        {
            GameObject go = Instantiate(clone);
            if (!active)
            {
                go.SetActive(active);
            }
            if (parent)
            {
                go.transform.SetParent(parent);
            }
            return go;
        }
        Debug.Log("没有找到指定物体["+ key + "]");
        return null;
    }
    /// <summary>
    /// 将物体（transform）取出对象池
    /// </summary>
    /// <param name="tr"></param>
    /// <returns></returns>
    public GameObject OutPool(Transform tr,bool active=true)
    {
        return OutPool(tr.gameObject,active);
    }
    /// <summary>
    /// 将物体放入对象池
    /// </summary>
    /// <param name="go"></param>
    /// <param name="active"></param>
    public void InPool(GameObject go,bool active=false)
    {
        go.SetActive(active);
        //为了去掉（clone）后缀
        string key = go.name.Split('(')[0];
        go.transform.SetParent(transform);
        if (pool.ContainsKey(key))
        {
            pool[key].Enqueue(go);
        }
        else
        {
            Queue<GameObject> que = new Queue<GameObject>();
            que.Enqueue(go);
            pool.Add(key, que);
        }
        if (go.GetComponent<MonsterBase>())
        {
            Debug.Log("INPOOL-----------"+key);
        }
    }
    /// <summary>
    /// 将物体（transform）放入对象池
    /// </summary>
    /// <param name="tr"></param>
    public void InPool(Transform tr,bool active=false)
    {
        InPool(tr.gameObject, active);
    }

    /// <summary>
    /// 获得一个指定名称的空物体
    /// </summary>
    /// <param name="name"></param>
    /// <param name="parent"></param>
    /// <returns></returns>
    public GameObject GetEmptyGO(string name,Transform parent=null)
    {
        if (pool.ContainsKey(name))
        {
            if (pool[name].Count > 0)
            {
                GameObject go=pool[name].Dequeue();
                go.SetActive(true);
                if (parent != null)
                {
                    go.transform.SetParent(parent);
                }
                return go;
            }
        }
        else
        {
            Queue<GameObject> que = new Queue<GameObject>();
            pool.Add(name,que);
        }
        GameObject g = new GameObject(name);
        if (parent != null)
        {
            g.transform.SetParent(parent);
        }
        return g;
    }
    /// <summary>
    /// 清空某个物体的池，可选是否删除该key
    /// </summary>
    /// <param name="name"></param>
    /// <param name="delete"></param>
    public void Clear(string name,bool delete=false)
    {
        if (pool.ContainsKey(name))
        {
            pool[name].Clear();
            if (delete)
            {
                pool.Remove(name);
            }
        }
    }
    /// <summary>
    /// 清空所有对象池，可选是否删除所有key
    /// </summary>
    /// <param name="delete"></param>
    public void ClearAll(bool delete=false)
    {
        if (delete)
        {
            pool = new Dictionary<string, Queue<GameObject>>();
            return;
        }
        foreach(string k in pool.Keys)
        {
            pool[k].Clear();
        }
    }
}
